#include <iostream>
#include <string>
#include <unordered_map>
#include <cmath>

bool is_prime(int num) {
    if (num < 2) {
        return false;
    }
    for (int i = 2; i <= sqrt(num); i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

int closest_prime_number(std::string words) {
    std::unordered_map<std::string, int> numbers = {
        {"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4},
        {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9}
    };

    try {
        std::string num_str;
        for (size_t i = 0; i < words.length(); i++) {
            if (words[i] != ' ') {
                num_str += std::to_string(numbers[words.substr(i, 4)]);
                i += 4;
            }
        }

        int num = std::stoi(num_str);
        if (num >= 100 && num <= 999) {
            int lower = num;
            int upper = num;
            while (true) {
                lower--;
                upper++;
                if (is_prime(lower)) {
                    return lower;
                }
                if (is_prime(upper)) {
                    return upper;
                }
            }
        } else {
            return -1; // Wrong input
        }
    } catch (...) {
        return -1; // Wrong input
    }
}

int main() {
    std::string input;
    std::cout << "Введите три слова: ";
    std::getline(std::cin, input);

    int result = closest_prime_number(input);
    if (result != -1) {
        std::cout << result << std::endl;
    } else {
        std::cout << "Wrong input" << std::endl;
    }

    return 0;
}
